package edu.info.manager.controller;

import edu.info.manager.domain.Student;
import edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {

    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    //开启学生管理系统并展示学生管理系统菜单
    public void start() {

        studentLooper:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查询");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢你使用学生管理系统，再见");
                    break studentLooper;
                default:
                    System.out.println("请输入正确数字");
                    break;
            }
        }

    }

    private void updateStudent() {
        String uid = inputStudentId();

        /*System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student(uid,name,age,birthday);*/

        Student stu = inputStudentInfo(uid);

        studentService.updateStudent(uid,stu);
        System.out.println("修改成功");

    }

    private void deleteStudentById() {
        /*String id;
        while(true){
            System.out.println("请输入要删除的学生id：(若退出请输入-1)");
            id = sc.next();
            if("-1".equals(id)){
                return;
            }
            boolean exist = studentService.isExist(id);
            if(!exist){
                System.out.println("查无此人，请重新输入：");
            }else {
                break;
            }
        }*/
        String id = inputStudentId();
        //调用业务员中同名方法根据id删除学生
        studentService.deleteStudentById(id);
        //提示删除成功
        System.out.println("删除成功");
    }

    public String inputStudentId(){
        String id;
        while(true) {
            System.out.println("请输入学生id：");
            id = sc.next();
            boolean exist = studentService.isExist(id);
            if (!exist) {
                System.out.println("查无此人，请重新输入：");
            } else {
                break;
            }
        }

        return id;

    }

    public Student inputStudentInfo(String id){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student(id,name,age,birthday);
        return stu;
    }

    private void findAllStudent() {
        //1. 调用业务员中的获取方法，得到学生对象数据
        Student[] stus = studentService.findAllStudent();
        //2、判断数组的内存地址对否为null
        if (stus == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        //3、遍历数组，获取学生信息并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
            }
        }
    }

    private void addStudent() {

        //Scanner sc = new Scanner(System.in);
        String id;
        //接受数据
        while (true) {
            System.out.println("请输入学生学号：");
            id = sc.next();
            boolean result = studentService.isExist(id);
            if (result) {
                System.out.println("学号已被占用，请重新输入");
            } else {
                break;
            }
        }

        /*System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        //封装数据
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setBirthday(birthday);*/
        Student student = inputStudentInfo(id);


        boolean flag = studentService.add(student);

        if (flag == false) {
            System.out.println("添加失败");
        } else {
            System.out.println("添加成功");
        }

    }

}
